package cn.owng.server.platform.controller.api.game;

import cn.owng.server.platform.model.dto.GameDTO;
import cn.owng.server.platform.model.http.request.DeleteByIdReq;
import cn.owng.server.platform.model.http.request.GameGameSaveOrUpdateReq;
import cn.owng.server.platform.model.http.request.PaginateReq;
import cn.owng.server.platform.model.http.response.ResponseBodyEntity;
import cn.owng.server.platform.model.http.response.ResponseCode;
import cn.owng.server.platform.model.vo.GameVO;
import cn.owng.server.platform.model.vo.IdVO;
import cn.owng.server.platform.model.vo.NullVO;
import cn.owng.server.platform.model.vo.PageVO;
import cn.owng.server.platform.service.DtoToVoMapper;
import cn.owng.server.platform.service.IGameGameService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 微信公众号请关注: 开源螺丝钉
 * <br>
 * 码云请关注: <a href="https://gitee.com/xiyoufang">https://gitee.com/xiyoufang</a>
 * <br>
 * 哔哩哔哩请关注: <a href="https://space.bilibili.com/438927834">https://space.bilibili.com/438927834</a>
 *
 * @author xiyoufang
 */
@RestController
@RequestMapping("/api/game/game")
public class GameGameController {

    private final IGameGameService gameGameService;

    public GameGameController(IGameGameService gameGameService) {
        this.gameGameService = gameGameService;
    }

    @PostMapping("/paginate")
    public ResponseEntity<ResponseBodyEntity<PageVO<GameVO>>> paginate(@RequestBody @Validated PaginateReq paginateReq) {
        IPage<GameDTO> paginate = this.gameGameService.paginate(paginateReq);
        return ResponseEntity.ok(ResponseBodyEntity.create(ResponseCode.OK, new PageVO<GameVO>()
                .setTotal(paginate.getTotal())
                .setRecords(paginate.getRecords().stream().map(DtoToVoMapper.INSTANCE::gameDTOToVO)
                        .collect(Collectors.toList())), "查询成功。"));
    }

    @PostMapping("/delete")
    public ResponseEntity<ResponseBodyEntity<NullVO>> delete(@RequestBody @Validated DeleteByIdReq deleteByIdReq) {
        boolean r = this.gameGameService.delete(Long.valueOf(deleteByIdReq.getId()));
        return ResponseEntity.ok(ResponseBodyEntity.create(ResponseCode.OK, new NullVO(), r ? "删除成功。" : "删除失败。"));
    }

    @PostMapping("/saveOrUpdate")
    public ResponseEntity<ResponseBodyEntity<IdVO>> saveOrUpdate(@RequestBody @Validated GameGameSaveOrUpdateReq gameGameSaveOrUpdateReq) {
        GameDTO gameDTO = this.gameGameService.saveOrUpdate(gameGameSaveOrUpdateReq);
        return ResponseEntity.ok(ResponseBodyEntity.create(ResponseCode.OK,
                new IdVO().setId(String.valueOf(gameDTO.getGameId())),
                Objects.equals(gameDTO.getGameId(), gameGameSaveOrUpdateReq.getGameId()) ? "更新成功。" : "新增成功。"));
    }
}
